//
//  NRWRequest.m
//  NRWMall
//
//  Created by 邢毅 on 2018/5/9.
//  Copyright © 2018年 com.anpxd. All rights reserved.
//

#import "NRWBaseRequest.h"

@interface NRWBaseRequest()<YTKRequestAccessory>

@end

@implementation NRWBaseRequest

+ (instancetype)requestWithDelegate:(id<YTKRequestDelegate>)delegate {
    
    NRWBaseRequest *que = [[self alloc] init];
    que.delegate = delegate;
    [que addAccessory:que];
    return que;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        
        [self addAccessory:self];
    }
    return self;
}

- (NSDictionary<NSString *,NSString *> *)requestHeaderFieldValueDictionary {
    
    return [self getHeader];
}

- (void)requestWillStart:(YTKRequest *)request {
    
    
}

- (void)requestWillStop:(YTKRequest *)request {
    
    NSLog(@"request will stop");
    
//    if (request.responseStatusCode == 715) {
//
//    }
//
//
//    //保存Authorization
//    [self saveAuthorizationWithHeader:request.responseHeaders];
//
    [self logRequest:request];
}

/**< 保存Authorization */
- (void)saveAuthorizationWithHeader:(NSDictionary *)header {

//    if (isDictionaryClass(header)) {
//
//        NSString *authorizationStr = header[@"Authorization"];
//
//        if (isNotEmptyString(authorizationStr)) {
//
//            [[NSUserDefaults standardUserDefaults] setObject:authorizationStr forKey:authorizationKey];
//        }
//    }
}

- (NSDictionary *)getHeader {
    
//    NSString *authorizationStr = [[NSUserDefaults standardUserDefaults] objectForKey:authorizationKey];
//
//    if (isNotEmptyString(authorizationStr)) {
//        return @{@"source": @"iOS_client",
//                 @"version": getAppVersion(),
//                 @"Authorization": authorizationStr
//                 };
//    } else {
        return @{@"source": @"iOS_client",
                 @"version": getAppVersion()
                 };
//    }
}

- (void)logRequest:(YTKRequest *)request {
    
    NSLog(@"==============================LogStart===================================");
    if (request.error) {
        NSLog(@"url = %@ \nparams = %@\n error = %@", request.requestUrl, request.requestArgument, request.error.localizedDescription);
    } else {
        NSLog(@"url = %@ \nparams = %@\n responseObject = %@", request.requestUrl, request.requestArgument, request.responseObject);
    }
    NSLog(@"==============================LogEnd===================================");
}

@end
